package com.google.sydym6.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.sydym6.R;
import com.google.sydym6.controllers.IPlayRadioController;
import com.google.sydym6.controllers.PlayRadioController;
import com.google.sydym6.logic.IRadioScrobblerModel;
import com.google.sydym6.logic.RadioScrobblerModel;
import com.google.sydym6.logic.managers.RadioManager;
import com.google.sydym6.logic.observers.IPlayerStateObservable;
import com.google.sydym6.logic.observers.IPlayerStateObserver;
import com.google.sydym6.logic.observers.IRecognizeStatusObservable;
import com.google.sydym6.logic.observers.IRecognizeStatusObserver;

public class AbstractMiniPlayerActivity extends FragmentActivity implements IRecognizeStatusObserver, IPlayerStateObserver {


	static final String TAG = "myLogs";

	ViewPager pager;
	PagerAdapter pagerAdapter;
	View radioMiniPlayerView;
	IRadioScrobblerModel model;
	IPlayRadioController controller;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v("AbstractMiniPlayerActivity", "enter AbstractMiniPlayerActivity onCreate");
		super.onCreate(savedInstanceState);
		
	    model = RadioScrobblerModel.getInstance();

	    Log.v("Multitreading", "SongInfoActivity: Creating new observers");
	    IRecognizeStatusObservable radioManagerStatusObservable = (IRecognizeStatusObservable) model.getRadioManager();
		radioManagerStatusObservable.addObserver((IRecognizeStatusObserver)this);
		
		IPlayerStateObservable radioManagerStateObservable = (IPlayerStateObservable) model.getRadioManager();
		radioManagerStateObservable.addObserver((IPlayerStateObserver)this);
	}
	
	protected void initializeMiniPlayer() {
		LinearLayout miniplayer = (LinearLayout) findViewById(R.id.radioMiniPlayer);
	    miniplayer.setVisibility(View.VISIBLE);
	    
	    ImageButton playPauseButton = (ImageButton) findViewById(R.id.playPauseButton);
	    
	    playPauseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("playPauseButton was clicked");
				
				controller.playStopRadio();
			}
		});
	}
	
	@Override
	public void updatePlayerState() {
  		runOnUiThread(new Runnable() {

			@Override
			public void run() {
		  		ImageButton playPauseButton = (ImageButton) findViewById(R.id.playPauseButton);
		  		if(model.getRadioManager().isPlaying()) {
		  			playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
		  			
		  			ProgressBar radioLoadingBar = (ProgressBar) findViewById(R.id.radioLoadingBar);
					radioLoadingBar.setVisibility(View.VISIBLE);
		  		} else {
		  			playPauseButton.setImageResource(android.R.drawable.ic_media_play);
		  			
		  			ProgressBar radioLoadingBar = (ProgressBar) findViewById(R.id.radioLoadingBar);
					radioLoadingBar.setVisibility(View.GONE);
		  		}
			}
  			
  		});
	}

	@Override
	public void updateRecognizeStatus() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				
				TextView statusTextView = (TextView) findViewById(R.id.radioArtistTitleTextView);
				statusTextView.setVisibility(View.VISIBLE);
		  		
		  		String status = model.getRadioManager().getRecognizeStatus();
		  		//System.out.println("statusTextView == null: " + (statusTextView == null));
		  		System.out.println("updateRecognizeStatus: " + status);
		  		
		  		if(status != null) {

		  			ProgressBar radioLoadingBar = (ProgressBar) findViewById(R.id.radioLoadingBar);
		  			if (status.equals(RadioManager.RADIO_LOADING)) {
						radioLoadingBar.setVisibility(View.VISIBLE);
		  			} else {
						radioLoadingBar.setVisibility(View.GONE);
		  			}
		  			
			  		if(status.equals(RadioManager.RECOGNIZING_SUCCESS)) {
			  			statusTextView.setText(model.getRadioManager().getRecognizedArtistTitle());
			  		} else { 
			  			statusTextView.setText(status);
			  		}
		  		}
			}
  			
  		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.v("AbstractMiniPlayerActivity", "onResume model.getRadioManager() = " + model.getRadioManager());
		controller = new PlayRadioController(this);
		Log.v("AbstractMiniPlayerActivity", "controller = " + controller);
		updatePlayerState();
		updateRecognizeStatus();
		if(model.getRadioManager().getRadio() != null) {
			setRadioName();
		}
	}
	
	@Override
	protected void onDestroy() {
		  super.onDestroy();
		  
		  Log.v("Multitreading", "SongInfoActivity: Deleting observers");
		  IRecognizeStatusObservable radioManagerStatusObservable = (IRecognizeStatusObservable) model.getRadioManager();
		  radioManagerStatusObservable.removeObserver((IRecognizeStatusObserver)this);
			
		  IPlayerStateObservable radioManagerStateObservable = (IPlayerStateObservable) model.getRadioManager();
		  radioManagerStateObservable.removeObserver((IPlayerStateObserver)this);
	}
	  
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	} 
	  
  	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	  	switch(item.getItemId()) {
	  		case R.id.songMiniPlayer :
	  			Intent intent = new Intent(this, SettingsActivity.class);
	  			startActivity(intent);
	  			return true;
	  		default :
	  			return super.onOptionsItemSelected(item);
	  	}
	}

  	public void setRadioName() {
		TextView statusText = (TextView) findViewById(R.id.radioNameTextView);
		statusText.setText(model.getRadioManager().getRadio().getTitle());
		Log.v("AbstractMiniPlayerActivity", "radioname = " + model.getRadioManager().getRadio().getTitle());
  	}

}
